import axios from 'axios';
import { LoginParam, User, PasswordParam } from '@/type/Auth';
import { Permission } from '@/type/Permission';
import { getAuth } from '.';

async function login(loginParams: LoginParam): Promise<string> {
    return axios.post('/login', loginParams);
}

async function otpLogin(otp: string): Promise<string> {
    return axios.post('/otp-login', { otp });
}

async function logout(): Promise<{}> {
    return axios.post('/logout', {}, getAuth());
}

async function getUser(): Promise<User> {
    return axios.get('/users/logged', getAuth());
}

async function updateUser(user: Partial<User & PasswordParam>): Promise<User> {
    return axios.put(`/users/${user.id}`, user, getAuth());
}

async function getLoggedUserPermission(): Promise<Permission[]> {
    return axios.get('/users/logged/permissions', getAuth());
}

export default {
    login,
    otpLogin,
    logout,
    getUser,
    updateUser,
    getLoggedUserPermission,
};
